#include "dominion.h"
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>

// test scoreFor

void updateScore(int card, int* score)
{
	if (card == curse) 
	{ 
		*score = *score - 1; 
	}
	if (card == estate) 
	{ 
		*score = *score + 1; 
	}
	if (card == duchy) 
	{ 
		*score = *score + 3; 
	}
	if (card == province) 
	{ 
		*score = *score + 6; 
	}
	if (card == great_hall) 
	{ 
		*score = *score + 1; 
	}
}

int testScoreFor()
{
	printf("Testing scoreFor()");
	
	struct gameState *G = newGame();

	// init random
	srand(time(NULL));
	int r = rand();
	//populate card options
	int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, sea_hag, tribute, smithy};

	//populate g
	initializeGame(2, k, r, G);
	int i;
	// This is to match the 3 estates that we start out with
	int score = 3;
	int num_gardens = 0;
	printf("Testing by randomly inserting cards into various hand/deck/discard of player 0.\n");
	for(i = 0; i < 1000; i++)
	{
		int location = rand() % 3;
		int card = rand() % treasure_map;
		

		//randomly add the card to the deck, hand, or discard
		if(location == 0 && G->handCount[0] < MAX_HAND)
		{
			G->hand[0][G->handCount[0]] = card;
			G->handCount[0]++;
		}
		else if(location == 1 && G->discardCount[0] < MAX_DECK)
		{
			G->discard[0][G->discardCount[0]] = card;
			G->discardCount[0]++;
		}
		else if(G->deckCount[0] < MAX_DECK)
		{
			G->deck[0][G->deckCount[0]] = card;
			G->deckCount[0]++;
		}
		else
		{
			break;
		}

		updateScore(card, &score);
		//Then update the score from the card, excluding gardens
		//which we will tally at the very end

		if(card == gardens)
		{
			num_gardens++;
		}

	}
	// incorperate gardens
	int total_cards = G->deckCount[0] + G->discardCount[0] + G->handCount[0];
	score += (total_cards / 10)  * num_gardens;
	printf("Expected Score: %d\n", score);
	printf("Actual Score: %d\n", scoreFor(0, G));
	assert(scoreFor(0, G) == score);
	printf("Test passed.\n");
	printf("All tests passed.\n");
	return 0;
}

int main()
{
	return testScoreFor();
}
